<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Shoutbox/classes/PhpFox_ComponentList.class.php, $Author:  $ */



/** Shoutbox list component
 *
 * @package module.shoutbox
 * @version $Id:
 *
 */
class PhpFox_Mod_Shoutbox_ComponentList extends BaseComponent
{
    function process()
    {
        $oReq = &App::getRequest();
        $oSrvShoutbox = $this->_oModule->getService('Shoutbox');
        $aCond = array();

        // delete messages from period
        if($oReq->getInt('period'))
        {
            switch ($oReq->getInt('period'))
            {
                case 2:
                    $sInterval = mktime(0,0,0,date('m'),date('d')-1,date('y'));
                    break;
                case 3:
                    $sInterval = mktime(0,0,0,date('m'),date('d')-7,date('y'));
                    break;
                case 4:
                    $sInterval = mktime(0,0,0,date('m')-1,date('d'),date('y'));
                    break;
                case 5:
                    $sInterval = mktime(0,0,0,date('m'),date('d'),date('y')-1);
                    break;
                default:
                    $sInterval = mktime(0,0,0);
            }
            $oSrvShoutbox->deleteFromPeriod($sInterval);
            App::gotoUrl('', array(), App::format('Mod_Shoutbox.delete.item'), 'message', 'Shoutbox');
        }

        $iPage = $oReq->get('page', 1);
        $iPageSize = App::getParam('admin.pagesize');

        $iCnt = $this->oDatabase->getField("
        	SELECT COUNT(*) FROM ". App::getT('shoutbox') ."
        ");

		$iOffSet = $this->oDatabase->_getPageNum($iPage, $iPageSize, $iCnt);

        $aShout = $this->oDatabase->getRows("
        	SELECT s.shoutid, s.text, s.time, u.user FROM
        	". App::getT('shoutbox') ." AS s
        		LEFT JOIN ". App::getT('user') ." AS u ON(u.id = s.user_id)
			GROUP BY s.shoutid
			ORDER BY s.time DESC LIMIT ". $iOffSet .", ". $iPageSize ."
        ");

        $oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

        // delete single message
        if($iDeleteId = $oReq->getInt('del'))
        {
        	$this->oDatabase->_delete(App::getT('shoutbox'), "shoutid = '". $iDeleteId ."'");
            $oUrl = &App::getUrl();
			$oUrl->setParam('del', null);

        	App::gotoUrl(App::getPageName(), $oUrl->getParams(), App::format('Mod_Shoutbox.delete.item'), 'message', 'Shoutbox');
        }

        $oTpl = &$this->_oModule->getTemplate();

        $oTpl->assign(array(
                            'aParams'   => $this->_aParams,
                            'aInterval' => array(1=>App::format('core.today'),App::format('core.yesterday'),App::format('core.last.week'),App::format('core.last.month'),App::format('core.last.year')),
                            'aPaging'   => $oPager->getInfo(App::getUrl()),
                            'aShout'    => $aShout,
                            ));

        $sHtml = $oTpl->fetch('List.html');

        return $sHtml;
    }
}

?>